package cn.com.zs.android.util;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * 关于数字的工具类
 * @author 曾帅
 */
public class NumberUtil {

	/**
	 * 产生随机不重复数组
	 * 
	 * @param length
	 * @param number
	 * @return
	 */
	public static int[] noRepeatRandomNumber(int length, int max) {
		int[] randomNumber = new int[length];
		boolean[] boolTemp = new boolean[max];
		Random random = new Random();
		do {
			int number = random.nextInt(max);
			if (boolTemp[number] == false) {
				randomNumber[--length] = number;
				boolTemp[number] = true;
			}
		} while (length > 0);
		return randomNumber;
	}
	
	/**
	 * 合并两个数组, 并去除重复元素
	 * @param a
	 * @param b
	 * @return
	 */
	public static int[] mergerArrayAndNoRepeat(int[] one, int[] two) {
		for(int i = 0; i < two.length; i++) {
			boolean isRepeat = false;
			for(int j = 0; j < one.length; j++) {
				if (two[i] == one[j]) {
					isRepeat = true;
					break;
				}
			}
			if (isRepeat == false) {
				int[] temp = new int[one.length + 1];
				temp[one.length] = two[i];
				for(int j = 0; j < one.length; j ++) {
					temp[j] = one[j];
				}
				one = temp;
			}
		}
		return one;
	}
	
	/**
	 * 扩充数组长度
	 * @param array
	 * @param length	要扩充几位
	 * @return
	 */
	public static int[] extendArray(int[] array, int length, int defaultNumber) {
		int[] temp = new int[array.length + length];
		for(int i = 0; i < array.length; i ++) {
			temp[i] = array[i];
		}
		for(int i = array.length; i < temp.length; i ++) {
			temp[i] = defaultNumber;
		}
		return temp;
	}
	
	public static boolean[] extendArray(boolean[] array, int length) {
		boolean[] temp = new boolean[array.length + length];
		for(int j = 0; j < array.length; j ++) {
			temp[j] = array[j];
		}
		return temp;
	}
	
	public static int[] cancelElement(int[] array, int which) {
		List<Integer> tempList = new ArrayList<Integer>();
		for(int i = 0; i < array.length; i ++) {
			if (i == which) {
				continue;
			}
			tempList.add(array[i]);
		}
		int[] temp = new int[tempList.size()];
		for (int i = 0; i < tempList.size(); i ++) {
			temp[i] = tempList.get(i);
		}
		return temp;
	}
	
	/**
	 * 把数组里元素的数值依次加num
	 * @param array
	 * @param num
	 * @return
	 */
	public static int[] addArrayElement(int[] array, int num) {
		for(int i = 0; i < array.length; i ++) {
			array[i] = array[i] + 1;
		}
		return array;
	}
	
	/** 
	* 提供小数位四舍五入处理 
	* @param v 需要四舍五入的数字 
	* @param scale 小数点后保留几位 
	* @return 四舍五入后的结果 
	*/ 
	public static double round(double number, int scale) {
		String temp = "#,##0.";
		for (int i = 0; i < scale; i++) {
			temp += "0";
		}
		return Double.valueOf(new java.text.DecimalFormat(temp).format(number));
	}
}
